how to output only a certain length of a string in javascript

122

how to output only a certain length of a string in javascript -

var str = 'Some very long string';
if(str.length > 10) str = str.substring(0,10);

how to output only a certain length of a string in javascript -

var example = "I am too long string";
var result;

// Slice is JS function
result = example.slice(0, 10)+'...'; //if you need dots after the string you can add

how to find length of string in javascript without using length method -

function strLength(s) {
  var length = 0;
  while (s[length] !== undefined){
    length++;
  } 
  return length;
}

console.log(strLength("Hello")); // 5
console.log(strLength("")); // 0

Comments

Submit
0 Comments